package com.kiln.juxingqs.ui

import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.ZcjlAdapter
import com.kiln.juxingqs.api.bean.Zcjl
import com.kiln.juxingqs.databinding.ActivityBljmxBinding
import com.kiln.juxingqs.dialog.DateTimeDialog
import com.kiln.juxingqs.ui.vm.BljmxViewModel
import java.util.*


class BljmxActivity :
    BaseMiddleActivity<BljmxViewModel, ActivityBljmxBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_bljmx
    }

    //    val adapter: AvailableAssetsFragmentPageAdapter by lazy {
//        AvailableAssetsFragmentPageAdapter(supportFragmentManager)
//    }
    val adapter: ZcjlAdapter by lazy {
        ZcjlAdapter(this, mImgLoader)
    }

    override fun initViewModel(): BljmxViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        val calendar = Calendar.getInstance()
        val y = calendar.get(Calendar.YEAR)
        val m = calendar.get(Calendar.MONTH) + 1
        val d = calendar.get(Calendar.DAY_OF_MONTH)
        viewModel.endDate.value = "${y}-${if (m < 10) "0${m}" else m}-${d}"
    }

    override fun observerUI() {

        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
//            viewModel.holdOne()
            binding.emptyLayout.errorType = EmptyLayout.NODATA

        }
        val decoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
        decoration.setDrawable(dividerDrawable)
        binding.recyclerView.addItemDecoration(decoration)
        binding.recyclerView.adapter = adapter
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.holdOne()
            }
        }
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.record_list as MutableList<Zcjl.Record?>?
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 20) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
    }

    fun selectDate(index: Int) {
        DateTimeDialog(this@BljmxActivity).apply {
            ok = { y, m, d ->
                if (index == 0) {
                    viewModel.startDate.value = "${y}-${if (m < 10) "0${m}" else m}-${d}"
                } else {
                    viewModel.endDate.value = "${y}-${if (m < 10) "0${m}" else m}-${d}"
                }
            }
        }.show()
    }

    fun searchData() {
//        viewModel.pageOne.value = 0
//        viewModel.holdOne()
        binding.emptyLayout.errorType = EmptyLayout.NODATA

    }

    fun current() {
        val calendar = Calendar.getInstance()
        val y = calendar.get(Calendar.YEAR)
        val m = calendar.get(Calendar.MONTH) + 1
        val d = calendar.get(Calendar.DAY_OF_MONTH)

        viewModel.endDate.value = "${y}-${if (m < 10) "0${m}" else m}-${d}"

        var mm = m - 2
        var yy = y
        if (mm <= 0) {
            mm = 12 - mm
            yy--
        }
        viewModel.startDate.value =
            "${yy}-${if (mm < 10) "0${mm}" else mm}-01"

        searchData()
    }

    override fun initData() {
        searchData()
    }


}